//
//  MCStatusCell.swift
//  HXWeibo
//
//  Created by HX on 15/12/2.
//  Copyright © 2015年 HX. All rights reserved.
//

import UIKit

let MCStatusCellMargin : CGFloat = 8

/// 原创和转发cell的父类
class MCStatusCell: UITableViewCell
{
    // MARK:- 属性
    /// 微博模型
    var status: MCStatus?
        {
            didSet
            {
                topView.status = status
                
                // cell 微博内容
                contentLabel.text = status?.text
                
                // 设置配图视图的模型
                pictureView.status = status
                
                // 计算配图视图的宽高 calculable:计算
                let size = pictureView.calcViewSize()
//                print("配图的size-->\(size)")
                
                //设置配图视图的宽高
                pictureViewWidth?.constant = size.width
                pictureViewWidth?.constant = size.height
                
            }
        }
    /// 配图宽度约束
    var pictureViewWidth: NSLayoutConstraint?
    /// 配图高度约束
    var pictureViewHeight: NSLayoutConstraint?
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }
    
    // MARK: - 计算行高
    func rowHeight(status: MCStatus) -> CGFloat
    {
        // 根据模型来设置内容
        self.status = status
        
        // 更新约束
        self.layoutIfNeeded()
        
        // 获取底部视图的最大Y值(cell的高度)
        return CGRectGetMaxY(bottomView.frame)
    }
    
    // MARK: - 准备UI
     func prepareUI()
    {
        // 添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 添加约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.size.width, height: 55))
        
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: MCStatusCellMargin, y: MCStatusCellMargin))
        
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.size.width - 2 * MCStatusCellMargin))
        
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.size.width, height: 44), offset: CGPoint(x: -MCStatusCellMargin, y: MCStatusCellMargin))
        
    }
    
    // MARK: - 懒加载 顶部视图
    private lazy var topView: MCStatusTopView = MCStatusTopView()
    
    /// 微博文本内容
    lazy var contentLabel: UILabel =
    {
        let contentLabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
        
        // 设置换行
        contentLabel.numberOfLines = 0
        
        return contentLabel
    }()
    
    /// 底部视图
    lazy var bottomView: MCStatusBottomView = MCStatusBottomView()
    
    // 配图的宽高由 配图视图根据图片等多少来计算,如果没有图片将配图的宽高设置为0
    /// 配图视图
    lazy var pictureView: MCPictureView = MCPictureView()
    
}


































